import {SizeRoot} from "./SizeRoot";
import {PathArray, PathInfo} from "./PathInfo";
import {MatchColor} from "../colors/MatchColor";

class SinglePath extends SizeRoot {
    path: PathArray = [];
    constructor(path: PathArray) {
        super();
        this.path = path;
    }
    fillColor?: string;
    strokeColor?: string;
    closed: boolean = false;
    SetFillColor = (fill?: string) => {
        if (fill === undefined) return;
        if (MatchColor(fill)) this.fillColor = fill;
    }
    SetStrokeColor = (stroke?: string) => {
        if (stroke === undefined) return;
        if (MatchColor(stroke)) this.strokeColor = stroke;
    }

    SetRange = () => {
        const path = this.path;
        for (let p = 0; p < path.length; p++) {
            let vertex = path[p];
            if (p === 0) {
                this._minX = vertex[0]; this._maxX = vertex[0];
                this._minY = vertex[1]; this._maxY = vertex[1];
            }
            if (this._minX > vertex[0]) this._minX = vertex[0];
            if (this._maxX < vertex[0]) this._maxX = vertex[0];

            if (this._minY > vertex[1]) this._minY = vertex[1];
            if (this._maxY < vertex[1]) this._maxY = vertex[1];
        }
    }

    GetPathInfo = ():PathInfo => {
        return { name: "", closed: this.closed,
            path: this.path, minX: this._minX, minY: this._minY,
            maxX: this._maxX, maxY: this._maxY, fillStyle: this.fillColor,
            strokeStyle: this.strokeColor
        }
    }
}

export {SinglePath};